3 * Message blobs storage used by ResourceLoader.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Roan Kattouw
22 * @author Trevor Parscal
26 use Psr\Log\LoggerAwareInterface
;
27 use Psr\Log\LoggerInterface
;
28 use Psr\Log\NullLogger
;
29 use Wikimedia\Rdbms\Database
;
32 * This class generates message blobs for use by ResourceLoader modules.
34 * A message blob is a JSON object containing the interface messages for a certain module in
37 class MessageBlobStore
implements LoggerAwareInterface
{
39 /* @var ResourceLoader|null */
40 private $resourceloader;
43 * @var LoggerInterface
53 * @param ResourceLoader $rl
54 * @param LoggerInterface $logger
56 public function __construct( ResourceLoader
$rl = null, LoggerInterface
$logger = null ) {
57 $this->resourceloader
= $rl;
58 $this->logger
= $logger ?
: new NullLogger();
59 $this->wanCache
= ObjectCache
::getMainWANInstance();
64 * @param LoggerInterface $logger
66 public function setLogger( LoggerInterface
$logger ) {
67 $this->logger
= $logger;
71 * Get the message blob for a module
74 * @param ResourceLoaderModule $module
75 * @param string $lang Language code
78 public function getBlob( ResourceLoaderModule
$module, $lang ) {
79 $blobs = $this->getBlobs( [ $module->getName() => $module ], $lang );
80 return $blobs[$module->getName()];
84 * Get the message blobs for a set of modules
87 * @param ResourceLoaderModule[] $modules Array of module objects keyed by name
88 * @param string $lang Language code
89 * @return array An array mapping module names to message blobs
91 public function getBlobs( array $modules, $lang ) {
92 // Each cache key for a message blob by module name and language code also has a generic
93 // check key without language code. This is used to invalidate any and all language subkeys
94 // that exist for a module from the updateMessage() method.
95 $cache = $this->wanCache
;
97 // Global check key, see clear()
98 $cache->makeKey( __CLASS__
)
101 foreach ( $modules as $name => $module ) {
102 $cacheKey = $this->makeCacheKey( $module, $lang );
103 $cacheKeys[$name] = $cacheKey;
104 // Per-module check key, see updateMessage()
105 $checkKeys[$cacheKey][] = $cache->makeKey( __CLASS__
, $name );
108 $result = $cache->getMulti( array_values( $cacheKeys ), $curTTLs, $checkKeys );
111 foreach ( $modules as $name => $module ) {
112 $key = $cacheKeys[$name];
113 if ( !isset( $result[$key] ) ||
$curTTLs[$key] === null ||
$curTTLs[$key] < 0 ) {
114 $blobs[$name] = $this->recacheMessageBlob( $key, $module, $lang );
116 // Use unexpired cache
117 $blobs[$name] = $result[$key];
124 * @deprecated since 1.27 Use getBlobs() instead
127 public function get( ResourceLoader
$resourceLoader, $modules, $lang ) {
128 return $this->getBlobs( $modules, $lang );
133 * @param ResourceLoaderModule $module
134 * @param string $lang
135 * @return string Cache key
137 private function makeCacheKey( ResourceLoaderModule
$module, $lang ) {
138 $messages = array_values( array_unique( $module->getMessages() ) );
140 return $this->wanCache
->makeKey( __CLASS__
, $module->getName(), $lang,
141 md5( json_encode( $messages ) )
147 * @param string $cacheKey
148 * @param ResourceLoaderModule $module
149 * @param string $lang
150 * @return string JSON blob
152 protected function recacheMessageBlob( $cacheKey, ResourceLoaderModule
$module, $lang ) {
153 $blob = $this->generateMessageBlob( $module, $lang );
154 $cache = $this->wanCache
;
155 $cache->set( $cacheKey, $blob,
156 // Add part of a day to TTL to avoid all modules expiring at once
157 $cache::TTL_WEEK +
mt_rand( 0, $cache::TTL_DAY
),
158 Database
::getCacheSetOptions( wfGetDB( DB_REPLICA
) )
164 * Invalidate cache keys for modules using this message key.
165 * Called by MessageCache when a message has changed.
167 * @param string $key Message key
169 public function updateMessage( $key ) {
170 $moduleNames = $this->getResourceLoader()->getModulesByMessage( $key );
171 foreach ( $moduleNames as $moduleName ) {
172 // Uses a holdoff to account for database replica DB lag (for MessageCache)
173 $this->wanCache
->touchCheckKey( $this->wanCache
->makeKey( __CLASS__
, $moduleName ) );
178 * Invalidate cache keys for all known modules.
179 * Called by LocalisationCache after cache is regenerated.
181 public function clear() {
182 $cache = $this->wanCache
;
183 // Disable holdoff because this invalidates all modules and also not needed since
184 // LocalisationCache is stored outside the database and doesn't have lag.
185 $cache->touchCheckKey( $cache->makeKey( __CLASS__
), $cache::HOLDOFF_NONE
);
190 * @return ResourceLoader
192 protected function getResourceLoader() {
193 // Back-compat: This class supports instantiation without a ResourceLoader object.
194 // Lazy-initialise this property because most callers don't need it.
195 if ( $this->resourceloader
=== null ) {
196 $this->logger
->warning( __CLASS__
. ' created without a ResourceLoader instance' );
197 $this->resourceloader
= new ResourceLoader();
199 return $this->resourceloader
;
204 * @param string $key Message key
205 * @param string $lang Language code
208 protected function fetchMessage( $key, $lang ) {
209 $message = wfMessage( $key )->inLanguage( $lang );
210 $value = $message->plain();
211 if ( !$message->exists() ) {
212 $this->logger
->warning( 'Failed to find {messageKey} ({lang})', [
213 'messageKey' => $key,
221 * Generate the message blob for a given module in a given language.
223 * @param ResourceLoaderModule $module
224 * @param string $lang Language code
225 * @return string JSON blob
227 private function generateMessageBlob( ResourceLoaderModule
$module, $lang ) {
229 foreach ( $module->getMessages() as $key ) {
230 $messages[$key] = $this->fetchMessage( $key, $lang );
233 $json = FormatJson
::encode( (object)$messages );
234 // @codeCoverageIgnoreStart
235 if ( $json === false ) {
236 $this->logger
->warning( 'Failed to encode message blob for {module} ({lang})', [
237 'module' => $module->getName(),
242 // codeCoverageIgnoreEnd